package com.game.service.game.turntable.control;

import com.game.service.app.Utils;
import com.game.service.beans.UserInfo;
import com.game.service.game.turntable.PrizeItem;
import com.game.service.game.turntable.TurntableAddConfig;
import com.game.service.game.turntable.TurntableConfig;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;


/**
 * 玩家快输完的时候，进行拯救,
 * 1. 当日游戏总次数大于20次。
 * 2.累计下注数大于10000钻
 * 3.总返奖率低于90%,今日返奖率低于80%
 * 4.现有分数低于2000钻
 * 5.开一个随机90%~110%(10000钻)
 */
public class LifeSaving {
    private final TurntableConfig mTurntableConfig;
    private final LifeSavingRequirement mLifeSavingRequirement;
    private final int mBetNumber;

    public LifeSaving(UserInfo userInfo, int betNumber) {
        mTurntableConfig = TurntableAddConfig.getConfig();
        mLifeSavingRequirement = create(userInfo);
        mBetNumber = betNumber;
    }

    public PrizeItem createPrizeItem(long openPrize) {
        int rate = 0;
        if (mBetNumber != 0) {
            rate = (int) (openPrize / mBetNumber);
            if (rate > 150) {
                rate = 150;
            }
        }

        ArrayList<PrizeItem> prizeItems = mTurntableConfig.getTablePrizeItemList().getTable1prizeItems();
        for (PrizeItem prizeItem : prizeItems) {
            if (rate - prizeItem.getRate() >= 0) {//配置奖项从大到小，所以可以遍历出第一个符合条件的开奖项
                String log = String.format("满足条件,赠送大奖分数:%s,开奖item:%s", openPrize, prizeItem);
                LoggerFactory.getLogger(LifeSaving.class).info(log);
                return prizeItem;
            }
        }
        return prizeItems.get(15);//默认返回 0的中奖项
    }


    private PlayInfo getPlayInfo(ArrayList<int[]> prizeArray) {
        //传入玩家的游戏记录，今日游戏记录 ,返回今日历史返奖率与盈利情况
        int playCounts = 0;
        float win = 0;
        long bet = 0;

        for (int i = 0; i < prizeArray.size(); i++) {
            for (int j = 0; j < prizeArray.get(i).length; j++) {
                int counts = prizeArray.get(i)[j];
                playCounts += counts;//
                long betOnce = (long) counts * mTurntableConfig.getBetArray()[i];//统计总压
                bet += betOnce;
                float winOnce = counts * mTurntableConfig.getTablePrizeItemList().getTable1prizeItems().get(j).getRate() * mTurntableConfig.getBetArray()[i];//统计赢
                win += winOnce;

            }
        }
        float payoutRate;
        if (bet != 0) {
            payoutRate = win * 100 / bet;
        } else {
            payoutRate = 0;
        }
        long uWin = (long) (win - bet);

        PlayInfo playInfo = new PlayInfo();
        playInfo.setPlayCounts(playCounts);
        playInfo.setBetTotal(bet);
        playInfo.setPayoutRate(payoutRate);
        playInfo.setWin(uWin);

        return playInfo;
    }

    /**
     * 玩家快输完的时候，进行拯救,
     * 1. 当日游戏总次数大于20次。
     * 2.累计下注数大于2000钻
     * 3.总返奖率低于90%,今日返奖率低于80%
     * 4.现有分数低于2000钻
     * 5.开一个随机90%~110%(10000钻)
     */
    public long checkingLifeSaving() {
        if (mLifeSavingRequirement != null) {
            LifeSavingRequirement req = mLifeSavingRequirement;//玩家当前状态
            LifeSavingRequirement reqConfig = mTurntableConfig.getLifeSavingRequirement();//配置信息
            boolean isPlayCounts = req.getPlayInfoToday().getPlayCounts() >= reqConfig.getPlayInfoToday().getPlayCounts();//当日游戏总次数大于20次。
            boolean isBetTotal = req.getPlayInfoToday().getBetTotal() >= reqConfig.getPlayInfoToday().getBetTotal();//当日投注金额大于2000钻
            boolean isPayoutRateAll = req.getPlayInfoAll().getPayoutRate() <= reqConfig.getPlayInfoAll().getPayoutRate();//历史返奖率小于90%
            boolean isPayoutRateToday = req.getPlayInfoToday().getPayoutRate() <= reqConfig.getPlayInfoToday().getPayoutRate();//当日返奖率小于70%
            boolean isScoreCurrent = req.getScoreCurrent() <= reqConfig.getScoreCurrent();//当前分数小于5000
            boolean isScoreCurrentRate = req.getBelowScoreLoginRate() <= reqConfig.getBelowScoreLoginRate();//分数比例低于30%
            //小于5000，或者小于登录分数的30%;
            long winToday = req.getPlayInfoToday().getWin();//今日盈利
            boolean isOpen = isPlayCounts && isBetTotal && isPayoutRateAll && isPayoutRateToday && (isScoreCurrent || isScoreCurrentRate);
            //开奖分数=winToday
            if (isOpen && winToday < 0) {
                int savingRateMin = reqConfig.getSavingRateMin();//返奖比例为当日输分的百分比开奖(90~110)
                int savingRateMax = reqConfig.getSavingRateMax();//返奖比例为当日输分的百分比开奖(90~110)
                long openPrize = Math.abs(winToday) * Utils.random(savingRateMin, savingRateMax) / 100;
                System.out.println("满足条件,赠送大奖分数" + openPrize);
                return openPrize;
            }
        }
        return 0;
    }

    private LifeSavingRequirement create(UserInfo userInfo) {

        LifeSavingRequirement requirement = new LifeSavingRequirement();
        requirement.setScoreCurrent(userInfo.getScore());
        requirement.setScoreLogin(userInfo.getUserLoginInfo().getScore());
        int scoreLoginRate = (int) (requirement.getScoreCurrent() * 100 / (requirement.getScoreLogin() + 1));//当前分数/登录分数 ,的比例
        requirement.setBelowScoreLoginRate(scoreLoginRate);

        PlayInfo playInfoToday = getPlayInfo(userInfo.getPrize().getPrizeDailyTask().getPrizeArray());
        requirement.setPlayInfoToday(playInfoToday);
        PlayInfo playInfoAll = getPlayInfo(userInfo.getPrize().getPrizeList().getPrizeArray());
        requirement.setPlayInfoAll(playInfoAll);
        return requirement;

    }

}
